package com.credithc.signature.manager.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.signature.common.po.Template;
import com.credithc.signature.common.po.TemplateHistory;
import com.credithc.signature.common.ro.manager.ApprovalCallbackRo;
import com.credithc.signature.common.ro.manager.TemplateRo;
import com.credithc.signature.common.vo.manager.ChartVo;
import com.credithc.signature.manager.service.ChartService;
import com.credithc.signature.manager.service.TemplateMService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/api/scs/v2")
@Slf4j
public class TemplateApiController {

    @Autowired
    MessageFactory messageFactory;

    @Autowired
    TemplateMService templateService;

    @Autowired
    HttpServletResponse response;

    @Autowired
    private ChartService iconService;

    /**
     * 导出文件
     *
     * @param ids
     */
    @GetMapping("/template/export/{ids}")
    public void export(@PathVariable String ids) {
        templateService.export(ids, response, Template.class.getSimpleName());
    }

    /**
     * 修改审批状态--对外提供
     *
     * @param approvalCallbackRo
     * @return
     */
    @PostMapping("/template/auditSts")
    public Message updateAuditSts(@RequestBody ApprovalCallbackRo approvalCallbackRo) {
        try {
            log.info("中台调用更改模板审批状态"+ JSONObject.toJSONString(approvalCallbackRo));
            TemplateRo templateRo = new TemplateRo();
            templateRo.setTemplateNo(approvalCallbackRo.getCallerId() + "");
            templateRo.setAuditStatus(approvalCallbackRo.getStatus());
            templateService.updateAuditSts(templateRo);
            log.info("修改模板审批状态成功");
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, "模板审核状态修改成功！");
        } catch (Exception e) {
            log.error("修改模板审批状态失败", e);
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "模板审核状态修改失败: " + e.getCause());
        }
    }

    @GetMapping("/chart/date/between/count")
    public Message<String> betweenCount(Date start, Date end) {
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, iconService.betweenCount(start, end));
    }

    @GetMapping("/chart/info/saas")
    public Message<com.credithc.signature.manager.vo.ChartVo> chartInfo() {
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, iconService.chartInfo());
    }

    @GetMapping("/sign/use/count")
    public Message<Integer> signUseCount() {
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, iconService.signUseCount());
    }

    @PostMapping("/chart/between/count")
    public Message<ChartVo> dayStatistics(Date start, Date stop) {
        Map chartVo = iconService.bulid(start, stop);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, chartVo);
    }

    /**
     * 导出文件
     *
     * @param ids
     */
    @GetMapping("/template/history/export/{ids}")
    public void exportHistory(@PathVariable String ids) {
        templateService.export(ids, response, TemplateHistory.class.getSimpleName());
    }

}
